package com.uestc.ui;

import com.uestc.config.InitPath;
import com.uestc.utils.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipOutputStream;

public class RestoreView {
    private static List<File> selectedFiles;
    private static String selectedOption;
    private static String targetPrePath;

    public static void showRestoreDialog() {
        Stage dialog = new Stage();
        dialog.setTitle("打包");
        VBox vbox = new VBox(10,
                new Label("Select files:"),
                new Button("Browse"),
                new TextArea(), // 文件列表显示
                new Label("Select pack method:"),
                new ComboBox<>(),
                new Label("Select destination folder:"),
                new Button("Browse"),
                new TextField(), // 目标文件夹路径显示
                new HBox(30));

        Button browseFilesButton = (Button) vbox.getChildren().get(1);

        TextArea filesTextArea = (TextArea) vbox.getChildren().get(2); // 选中文件列表显示
        filesTextArea.setWrapText(true); // 允许文本自动换行
        filesTextArea.setPrefRowCount(10); // 设置首选行数，可以根据需要调整
        filesTextArea.setPrefHeight(100); // 设置首选高度，可以根据需要调整
        filesTextArea.setEditable(false); // 设置为只读，用户不能编辑内容

        ComboBox<String> packMethodComboBox = (ComboBox<String>) vbox.getChildren().get(4);

        Button browseFolderButton = (Button) vbox.getChildren().get(6);

        TextField folderTextField = (TextField) vbox.getChildren().get(7); // 目标文件夹路径显示
        folderTextField.setEditable(false);

        HBox lastBox = (HBox) vbox.getChildren().get(8);
        Button packButton = new Button("pack up");
        Button backButton = new Button("back");
        lastBox.getChildren().addAll(packButton, backButton);
        lastBox.setMargin(packButton, new Insets(0, 0, 30, 0));

        browseFilesButton.setOnAction(e -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setInitialDirectory(new File(InitPath.path));
            fileChooser.setTitle("Choose files");
            selectedFiles = fileChooser.showOpenMultipleDialog(dialog);
            // Handle selected files
            if (selectedFiles != null) {
                StringBuilder fileList = new StringBuilder();
                for (File file : selectedFiles) {
                    fileList.append(file.getName()).append("\n"); // 使用换行符
                }
                if (fileList.length() > 0) {
                    fileList.setLength(fileList.length() - 1);
                }
                filesTextArea.setText(fileList.toString());
            }
        });

        packMethodComboBox.getItems().addAll("only restore","tar", "huff", "aes");
        packMethodComboBox.setOnAction(e -> {
            selectedOption = packMethodComboBox.getValue();
        });

        browseFolderButton.setOnAction(e -> {
            // 选择一个文件夹
            DirectoryChooser directoryChooser = new DirectoryChooser();
            directoryChooser.setInitialDirectory(new File(InitPath.path));
            directoryChooser.setTitle("Choose one director");
            File file = directoryChooser.showDialog(dialog);
            targetPrePath = file.getPath();
            folderTextField.setText(targetPrePath);

        });

        backButton.setOnAction(e->{
            dialog.close();
            MainMenu.showLocalDialog();
        });

        packButton.setOnAction(e -> {

            if (selectedFiles == null || selectedOption == null || targetPrePath == null) {
                AlertUtil.showAlert("前置选项不能为空");
                return;
            } else if (selectedOption.equals("tar")) {
                PackUtil.restoreFile(selectedFiles,targetPrePath);
            } else if (selectedOption.equals("huff")) {
                HuffmanCoding.decompress(selectedFiles, targetPrePath);
            } else if (selectedOption.equals("aes")) {
                if (selectedFiles.size() > 1){
                    AlertUtil.showAlert("请只选择一个aes文件进行解密");
                }
                File file = selectedFiles.get(0);
                DialogUtil.showPasswordDialog("Please input Password", "Password");
                String password = DialogUtil.getPassword();
                if (password != null){
                    OpenSSLUtil.decryptFile(password, file.getPath(), targetPrePath + "\\" + file.getName().substring(0, file.getName().length() - 4));
                }else {
                    return;
                }
            }

            AlertUtil.showAlert("恢复验证成功");
        });


        Scene scene = new Scene(vbox, 600, 600);
        dialog.setTitle("File Packer");
        dialog.setScene(scene);
        dialog.show();
    }

}